<template>
  <view class="wallet">
    <view class="container">
      <view class="money">
        <view class="num">¥{{money.all_money.toFixed(2)}}</view>
        <view class="backMoney"><AtButton size="small"  type='primary' @click="getMoneyBack">立即返还</AtButton></view>
      </view>
      <view class="content">
        <view v-if="!noDataTrue" class="orderItem" v-for="item in OrderRecord" :key="item.withdrawal_code">
          <view class="topStatus">
            <view class="orderCode">提现码: <text style="font-size: 24rpx; position: relative; top: -5rpx;">{{item.withdrawal_code}}</text></view>
            <view class="status" :class="item.w_status == -1 ? 'cancleOrder': item.w_status == 0 ? 'willPayOrder' : item.w_status == 1 ? 'willGetGlass' : 'alreadyComplate'">
              {{item.w_status == -1 ?  '驳回' : item.w_status == 0 ? '待审核' : item.w_status == 1 ? '正在提现' : item.w_status == 2 ? '已到账' : ''}}
            </view>
          </view>
          <view class="middleContent">
            <view class="order-item">提现金额: ¥{{ item.total_price }}</view>
            <view class="order-item">提现时间: {{ item.withdrawal_date }}</view>
          </view>
        </view>
        <view class="no_list" v-else="noDataTrue"></view>
      </view>
    </view>
  </view>
</template>

<script setup>
import './index.scss'
import { AtButton } from 'taro-ui-vue3'
import {query_balance, do_refund, query_refund_record} from '../../http/api'
import {ref, reactive} from 'vue'
import Taro, {useDidShow} from '@tarojs/taro'
const money = reactive({
  all_money: 0,
  t_money: 0
})

const noDataTrue = ref(false)

const getMoneyBack = () => {
  if(money.t_money<=0) {
    Taro.showToast({
      title: `暂无可提现余额`,
      icon: 'none',
      duration: 2000
    })
    return
  }
  Taro.getSetting({
      withSubscriptions: true,
      success: (res) => {
        if (!res.subscriptionsSetting.mainSwitch
        || (res.subscriptionsSetting.itemSettings && res.subscriptionsSetting.itemSettings['kVGxFfk0ZW0aGbWr97PbM-bPDmsM8vWIiNAy1L5jEgk']) == 'reject'
      ) {
          Taro.showModal({
            title: '温馨提示',
            showCancel: false,
            content: '为了您能接收到退款提醒消息，请打开设置勾选订阅接受通知消息',
            success: function (res) {
              if (res.confirm) {
                Taro.openSetting({
                  success: function (res) {
                    Taro.showModal({title: '温馨提示',
                    content: '订阅消息通知，方便给您退款通知',
                    showCancel: false,
                    success: function (res) {
                      if (res.confirm) {
                        Taro.requestSubscribeMessage({
                          tmplIds: ['kVGxFfk0ZW0aGbWr97PbM-bPDmsM8vWIiNAy1L5jEgk'],
                          complete: function (Subscribe) {
                          }
                        })
                      } else if (res.cancel) {
                        console.log('用户点击取消')
                      }
                    }})
                  }
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }else {
          Taro.requestSubscribeMessage({
            tmplIds: ['kVGxFfk0ZW0aGbWr97PbM-bPDmsM8vWIiNAy1L5jEgk'],
            complete: function (Subscribe) {

            }
          })
          Taro.showModal({
            title: '余额提现',
            confirmColor:'#ffc600',
            content: `当前可提现金额为¥${money.t_money}, 是否进行余额提现？提现金额预计72小时内到账，请耐心等待~`,
            success: function (res) {
              if (res.confirm) {
                do_refund().then(money => {
                  if(money.code === '1000') {
                    Taro.showToast({
                      title: '提现申请成功，金额返还付款账户',
                      icon: 'success',
                      duration: 2000
                    })
                    getMoney()
                  } else {
                    Taro.showToast({
                      title: `${money.msg}`,
                      icon: 'error',
                      duration: 2000
                    })
                  }
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })

        }
      }
    })

}

function getMoney () {
  query_balance().then(res => {
    money.all_money = res.data.all_money
    money.t_money = res.data.t_money
  })
}
const OrderRecord = ref([])
function returnOrderRecord () {
  query_refund_record().then(res => {
    if (res.code == '1000') {
      OrderRecord.value = res.data
      // OrderRecord.value = [{
      //   "total_price": 2,
      //   "w_status": -1,
      //   "withdrawal_code": "W202405281641289269226xmo8n",
      //   "withdrawal_date": "2024-05-28 16:41:29"
      // }]
      noDataTrue.value =  !OrderRecord.value.length
    }
  })
}


function gotoPayHtml(code) {
  Taro.navigateTo({
    url: `/pages/orderDetail/index?o_order_code=${code}&o_is_refund=-1`
  })
}

useDidShow(() => {
  getMoney()
  returnOrderRecord()
})
Taro.showShareMenu({
  withShareTicket: true,
  menus: ['shareAppMessage', 'shareTimeline']
})
</script>
